<template>
  <div class="result-container">
    <!-- 1.顶部导航栏 -->
    <van-nav-bar
      left-arrow
      :title="`${$route.query.value}的结果`"
      @click-left="$router.go(-1)"
    >
    </van-nav-bar>
    <!-- 文章列表 -->
    <!-- 文章列表 -->
    <van-list
      class="search-list"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in list"
        :key="item.art_id.toString()"
        :title="item.title"
        @click="$router.push({ path: '/article', query: { value: art_id } })"
      />
    </van-list>
  </div>
</template>

<script>
import { getSearch } from "./home.js";
export default {
  name: "searchResult",
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
    };
  },

  methods: {
    async onLoad() {
      //发送请求
      const res = await getSearch({
        q: this.$route.query.value,
        page: this.page,
        per_page: 10,
      });
      //添加数据
      const arr = res.data.data.results;
      this.list.push(...arr);
      this.page++;
      console.log(arr);
      //将loading改为fasle
      this.loading = false;
      //判断数据是否全部加载
      if (arr.length === 0) {
        this.finished = true;
      }
    },
  },
};
</script>

<style lang='less'>
.result-container {
  height: 100vh;
  box-sizing: border-box;
  .search-list {
    height: 100%;
    overflow-y: scroll;
  }
}
</style>